package com.jin.webvideo.parser;

import com.jin.webvideo.js.ScriptExector;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;

import javax.script.ScriptException;
import java.io.IOException;

public class ParseScriptExector extends ScriptExector {
    public static class FuncResult{
        private String output;
        private String config;

        public String getConfig() {
            return config;
        }

        public String getOutput() {
            return output;
        }

        public void setConfig(String config) {
            this.config = config;
        }

        public void setOutput(String output) {
            this.output = output;
        }
    }
    public class Global{
        public void eval(String url) throws ScriptException, IOException {
        }
    }
    private static final String EXECFUNC = ";execFunc(%s, %s, %s)";
    private ParseSourceManager parseSourceManager;
    public ParseScriptExector(ParseSourceManager manager, ParserScriptPropertites config){
        super(manager, config.createBuilder());
        this.parseSourceManager = manager;
        putMember("GLOBAL", new Global());
    }

    public String execFunc(String scriptName, String methodName, String input, String config) throws ScriptException, IOException {
        String function = String.format(EXECFUNC, methodName, input, config);
        Source src = Source.newBuilder("js", function, scriptName).mimeType("application/javascript").build();
        Value val = super.evalSource(src);
        return val.asString();
    }
}